import os
from utils.constant import DATA_FOLDER
from datetime import datetime, timedelta


def get_latest_and_oldest_folders(directory):
    subdirectories = [d for d in os.listdir(directory) if os.path.isdir(os.path.join(directory, d))]

    if not subdirectories:
        return None, None

    try:
        sorted_subdirectories = sorted(subdirectories, key=lambda x: datetime.strptime(x, "%Y-%m-%d-%H"))
    except ValueError:
        sorted_subdirectories = sorted(subdirectories, key=lambda x: datetime.strptime(x.split('-')[0], "%Y-%m-%d-%H"))

    latest_folder = sorted_subdirectories[-1]
    oldest_folder = sorted_subdirectories[0]

    return latest_folder, oldest_folder

def check_valid(from_date_str, to_date_str):
    latest, oldest = get_latest_and_oldest_folders(DATA_FOLDER)
    if oldest <= from_date_str and to_date_str <= latest:
        return 1
    return 2

